<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Session;

use app\common\model\SystemMenu as SystemMenuModel;
use app\common\model\Site as SiteModel;

class Index extends Base
{
    public function index()
    {
        //站点遍历
        $SiteModel = new SiteModel();
        $site_res = $SiteModel -> order("orderid asc") -> select();

        //View::assign('admin_siteid',$site_res);
        View::assign('site_res',$site_res);
        return View::fetch();
    }

    //欢迎页
    public function welcome(){

        //用户数量
        $users_num = DB::name('users')->count();
//        //产品数量
//        $lists_num = DB::name('product')->where("status","=",1)->count();
//        //案列数量
//        $contact_num = DB::name('anli')->where("status","=",1)->count();
//        //展会
//        $order_num = DB::name('review')->where("status","=",1)->count();


        $version = Db::query('SELECT VERSION() AS ver');
        $config  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        View::assign('config',$config);

        //培训报名数量/职称学历报名数量 统计
        //本周开始时间戳
        $m = (int)date('m');
        $y = (int)date('y');
        $start = (int)date('d')-date('w')+1;
        $end = (int)date('d')-date('w')+7;
        $startTime = mktime(0,0,0,$m,$start,$y);
        $endTime = mktime(23,59,59,$m,$end,$y);
        $lists_res = DB::name('users')
            ->where('add_time','>=',$startTime)
            ->where('add_time','<=',$endTime)
            ->select()->toArray();
        $one = $two = $three = $four = $five = $six = $seven = 0;
        $one1 = $two1 = $three1 = $four1 = $five1 = $six1 = $seven1 = 0;
        foreach($lists_res as $k=>$v){
//            if($v['type'] == 1){
                //培训报名
                $w = date('w',$v['add_time']);
                if($w == 1){
                    $one++;
                }else if($w == 2){
                    $two++;
                }else if($w == 3){
                    $three++;
                }else if($w == 4){
                    $four++;
                }else if($w == 5){
                    $five++;
                }else if($w == 6){
                    $six++;
                }else if($w == 0){
                    $seven++;
                }
//            }else{
//                //职称学历
//                $w = date('w',$v['add_time']);
//                if($w == 1){
//                    $one1++;
//                }else if($w == 2){
//                    $two1++;
//                }else if($w == 3){
//                    $three1++;
//                }else if($w == 4){
//                    $four1++;
//                }else if($w == 5){
//                    $five1++;
//                }else if($w == 6){
//                    $six1++;
//                }else if($w == 0){
//                    $seven1++;
//                }
//            }
            $lists_res[$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }

//        $two_res = DB::name('product')
//            ->where('add_time','>=',$startTime)
//            ->where('add_time','<=',$endTime)
//            ->select()->toArray();
//        foreach($two_res as $k=>$v){
//            $w = date('w',$v['add_time']);
//            if($w == 1){
//                $one1++;
//            }else if($w == 2){
//                $two1++;
//            }else if($w == 3){
//                $three1++;
//            }else if($w == 4){
//                $four1++;
//            }else if($w == 5){
//                $five1++;
//            }else if($w == 6){
//                $six1++;
//            }else if($w == 0){
//                $seven1++;
//            }
//            $lists_res[$k]['add_time'] = date('Y-m-d',$v['add_time']);
//        }

        $data_str = '['.$one.', '.$two.', '.$three.', '.$four.', '.$five.', '.$six.', '.$seven.']';
//        $data_str1 = '['.$one1.', '.$two1.', '.$three1.', '.$four1.', '.$five1.', '.$six1.', '.$seven1.']';
        View::assign('data_str',$data_str);
//        View::assign('data_str1',$data_str1);

        //dump(session('act_list'));
        View::assign('users_num',$users_num);
//        View::assign('lists_num',$lists_num);
//        View::assign('contact_num',$contact_num);
//        View::assign('order_num',$order_num);
        return View::fetch();
    }

    //更换站点
    public function changeSite(){
        $param = input("param.");
        $site_id = $param['site_id'];

        Session::set('admin_siteid', $site_id);
        \think\facade\Session::save();

        return dyajaxReturn(1,"站点切换成功");
    }

    /*
     * 图片上传
     * @param $filenames  文件域
     * @param $position  存放目录
    */
    public function dyupload(){
        return dyuploadimg('dypic',input('imgpath/s'));
    }

    /*
     * 图片上传(带参数)
     * @param $filenames  文件域
     * @param $position  存放目录
    */
    /*public function dyuploadParam(){
        $id = input('param.id');
        return dyuploadimgParam('dypic',input('imgpath/s'),$id);
    }*/

    /*
     * 文件上传
     * @param $filenames  文件域
     * @param $position  存放目录
    */
    public function dyuploadfile(){
        return dyuploadfile('file',input('imgpath/s'));
    }

    public function dyuploadlayedit(){
        return dyuploadlayedit('file',input('imgpath/s'));
    }


    //视频上传
    public function dyuploadVideo()
    {
        return dyuploadVideo('dyvideo',input('imgpath/s'));
    }


    public function getLeftMenu(){
        $logo_img = SITE_URL.'/static/layuimini/images/logo.png';

        if(isset(config('webconfig')['cfg_ico'])){
            $logo_img = config('webconfig')['cfg_ico'];
        }

        $SystemMenuModel = new SystemMenuModel();
        $all_menu_res = $SystemMenuModel->where("show","=",1)->order('sort asc')->select()->toArray();
        $menuInfo = $SystemMenuModel ->leftMenuList(0,$all_menu_res);
        $left_menu = [
            'homeInfo' => [
                'title' => '首页',
                'href' => 'admin/index/welcome',
            ],
            'logoInfo' => [
                'title' => config("webconfig")['cfg_webname'],
                'image' => $logo_img,
                'href' => '',
            ],
            'menuInfo' =>$menuInfo
        ];
        return json($left_menu);

        /*$logo_img = SITE_URL.'/static/layuimini/images/logo.png';
        $left_menu = [
            'homeInfo' => [
                'title' => '首页',
                'href' => 'admin/index/welcome',
            ],
            'logoInfo' => [
                'title' => 'LAYUI MINI',
                'image' => $logo_img,
                'href' => '',
            ],

            'menuInfo' =>
                [
                [
                    'title' => '常规管理',
                    'icon' => 'fa fa-address-book',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '控制面板',
                            'href' => '',
                            'icon' => 'fa fa-laptop',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '管理员管理',
                                    'href' => 'admin/admin/list',
                                    'icon' => 'fa fa-user',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '角色管理',
                                    'href' => 'admin/role/list',
                                    'icon' => 'fa fa-tachometer',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '节点管理',
                                    'href' => 'admin/system/right_list',
                                    'icon' => 'fa fa-tachometer',
                                    'target' => '_self',
                                ],
                            ],
                        ],
                        [
                            'title' => '菜单管理',
                            'href' => 'page/menu.html',
                            'icon' => 'fa fa-window-maximize',
                            'target' => '_self',
                        ],
                        [
                            'title' => '系统设置',
                            'href' => 'page/setting.html',
                            'icon' => 'fa fa-gears',
                            'target' => '_self',
                        ],
                        [
                            'title' => '表格示例',
                            'href' => 'page/table.html',
                            'icon' => 'fa fa-file-text',
                            'target' => '_self',
                        ],
                        [
                            'title' => '表单示例',
                            'href' => '',
                            'icon' => 'fa fa-calendar',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '普通表单',
                                    'href' => 'page/form.html',
                                    'icon' => 'fa fa-list-alt',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '分步表单',
                                    'href' => 'page/form-step.html',
                                    'icon' => 'fa fa-navicon',
                                    'target' => '_self',
                                ],
                            ],
                        ],
                        [
                            'title' => '登录模板',
                            'href' => '',
                            'icon' => 'fa fa-flag-o',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '登录-1',
                                    'href' => 'page/login-1.html',
                                    'icon' => 'fa fa-stumbleupon-circle',
                                    'target' => '_blank',
                                ],
                                [
                                    'title' => '登录-2',
                                    'href' => 'page/login-2.html',
                                    'icon' => 'fa fa-viacoin',
                                    'target' => '_blank',
                                ],
                            ],
                        ],
                        [
                            'title' => '异常页面',
                            'href' => '',
                            'icon' => 'fa fa-home',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '404页面',
                                    'href' => 'page/404.html',
                                    'icon' => 'fa fa-hourglass-end',
                                    'target' => '_self',
                                ],
                            ],
                        ],
                        [
                            'title' => '其它界面',
                            'href' => '',
                            'icon' => 'fa fa-snowflake-o',
                            'target' => '',
                            'child' => [
                                [
                                    'title' => '按钮示例',
                                    'href' => 'page/button.html',
                                    'icon' => 'fa fa-snowflake-o',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '弹出层',
                                    'href' => 'page/layer.html',
                                    'icon' => 'fa fa-shield',
                                    'target' => '_self',
                                ],
                            ],
                        ],
                    ],
                ],
                [
                    'title' => '组件管理',
                    'icon' => 'fa fa-lemon-o',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '图标列表',
                            'href' => 'page/icon.html',
                            'icon' => 'fa fa-dot-circle-o',
                            'target' => '_self',
                        ],
                        [
                            'title' => '图标选择',
                            'href' => 'page/icon-picker.html',
                            'icon' => 'fa fa-adn',
                            'target' => '_self',
                        ],
                        [
                            'title' => '颜色选择',
                            'href' => 'page/color-select.html',
                            'icon' => 'fa fa-dashboard',
                            'target' => '_self',
                        ],
                        [
                            'title' => '下拉选择',
                            'href' => 'page/table-select.html',
                            'icon' => 'fa fa-angle-double-down',
                            'target' => '_self',
                        ],
                        [
                            'title' => '文件上传',
                            'href' => 'page/upload.html',
                            'icon' => 'fa fa-arrow-up',
                            'target' => '_self',
                        ],
                        [
                            'title' => '富文本编辑器',
                            'href' => 'page/editor.html',
                            'icon' => 'fa fa-edit',
                            'target' => '_self',
                        ],
                        [
                            'title' => '省市县区选择器',
                            'href' => 'page/area.html',
                            'icon' => 'fa fa-rocket',
                            'target' => '_self',
                        ],
                    ],
                ],
                [
                    'title' => '其它管理',
                    'icon' => 'fa fa-slideshare',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '多级菜单',
                            'href' => '',
                            'icon' => 'fa fa-meetup',
                            'target' => '',
                            'child' => [
                                [
                                    'title' => '按钮1',
                                    'href' => 'page/button.html?v=1',
                                    'icon' => 'fa fa-calendar',
                                    'target' => '_self',
                                    'child' => [
                                        [
                                            'title' => '按钮2',
                                            'href' => 'page/button.html?v=2',
                                            'icon' => 'fa fa-snowflake-o',
                                            'target' => '_self',
                                            'child' => [
                                                [
                                                    'title' => '按钮3',
                                                    'href' => 'page/button.html?v=3',
                                                    'icon' => 'fa fa-snowflake-o',
                                                    'target' => '_self',
                                                ],
                                                [
                                                    'title' => '表单4',
                                                    'href' => 'page/form.html?v=1',
                                                    'icon' => 'fa fa-calendar',
                                                    'target' => '_self',
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                        [
                            'title' => '失效菜单',
                            'href' => 'page/error.html',
                            'icon' => 'fa fa-superpowers',
                            'target' => '_self',
                        ],
                    ],
                ],
            ],
        ];

        return json($left_menu);
        */
    }
}
